package com.pig4cloud.mall.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.mall.product.entity.SpuInfo;
import com.pig4cloud.mall.product.service.SpuInfoService;
import com.pig4cloud.mall.product.vo.SpuAddVo;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * spu信息
 *
 * @author fangyu
 * @date 2020-11-25 14:58:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/spuinfo")
@Api(value = "spuinfo", tags = "spu信息管理")
public class SpuInfoController {

	private final SpuInfoService spuInfoService;

	/**
	 * 分页查询
	 *
	 * @param params    查询参数
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('product_list_get')")
	public R list(@RequestParam Map<String, Object> params) {
		return spuInfoService.queryByCondition(params);
	}

	/**
	 * 通过spuId 查看该商品详情
	 *
	 * @param spuId spuId
	 * @return
	 */
	@GetMapping("/detail/{spuId}")
	public R detail(@PathVariable("spuId") Long spuId) {
		return spuInfoService.queryDetailBySpuId(spuId);
	}


	/**
	 * 分页查询
	 *
	 * @param page    分页对象
	 * @param spuInfo spu信息
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('product_list_get')")
	public R getSpuInfoPage(Page page, SpuInfo spuInfo) {
		return R.ok(spuInfoService.page(page, Wrappers.query(spuInfo)));
	}


	/**
	 * 通过id查询spu信息
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('product_list_get')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(spuInfoService.getById(id));
	}

	/**
	 * 新增spu 商品
	 *
	 * @param spuAddVo spu商品信息
	 * @return R
	 */
	@ApiOperation(value = "新增spu商品信息", notes = "新增spu商品信息")
	@SysLog("新增spu商品信息")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('product_list_add')")
	public R save(@RequestBody SpuAddVo spuAddVo) {
		return spuInfoService.addSpu(spuAddVo);
	}

	/**
	 * 修改spu信息
	 *
	 * @param spuInfo spu信息
	 * @return R
	 */
	@ApiOperation(value = "修改spu信息", notes = "修改spu信息")
	@SysLog("修改spu信息")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('product_list_edit')")
	public R updateById(@RequestBody SpuInfo spuInfo) {
		return R.ok(spuInfoService.updateById(spuInfo));
	}

	/**
	 * 通过id删除spu信息
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除spu信息", notes = "通过id删除spu信息")
	@SysLog("通过id删除spu信息")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('product_list_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(spuInfoService.removeById(id));
	}

}
